<?php

namespace Admin\Controller;
use Think\Controller;
use Think\Log;
use Org\Util\Page; 

class ReplyMessageController extends BaseController {
	function _initialize() {
		parent::_initialize ();
	}

	public function index() {
		$msg=M("wx_reply");

		$keyword=I('keyword');
        $where="";
		if($keyword)
		{
		   $where="`title` like '%".$keyword."%' or `replaytype` like '%".$keyword."%' or `keyword` like '%".$keyword."%'";
		} 
		
	    $count =$msg->where($where)->count(); // 查询满足要求的总记录数

		$Page = new Page ($count, 12 ); // 实例化分页类 传入总记录数和每页显示的记录数
		$Page -> setConfig('header', '条记录');
		$Page -> setConfig('theme', '<li><a>%totalRow% %header%</a></li> <li>%upPage%</li> <li>%downPage%</li> <li>%first%</li>  <li>%prePage%</li>  <li>%linkPage%</li>  <li>%nextPage%</li> <li>%end%</li> ');//(对thinkphp自带分页的格式进行自定义)
		$show = $Page->show (); // 分页显示输出

		$message=$msg->where($where)->limit( $Page->firstRow . ',' . $Page->listRows )->order("replyid desc")->select(); 

		$this->assign ( "message", $message );
		$this->assign ( "page", $show ); // 赋值分页输出 
		$this->display ();
	}
	
	public function add()
	{
		$replyid=I("id");
		
		$replyid= isset($replyid)?$replyid:0;
		$replyid =empty($replyid)?0:$replyid;
		
		//$ret=array();
		if($replyid>0)
		 $ret= M("wx_reply")->where("replyid=$replyid")->find();
		
		$wxmenu=M("wx_menu")->where("status=1")->order("listorder desc")->select();
		$this->assign ( "entity", $ret );
		$this->assign ( "wxmenu", $wxmenu );
		$this->display ();
	}
 
   
	public function addmessage() {
		$data = $_POST;
		if (count($_FILES)>0 && $_FILES ['picurl'] ['name'] !==null) {
			$img = $this->upload ();
			$picurl = $img [0] [savename];
			$data ["picurl"] = $picurl;
		}

		$url=$data['url'];
		if(!empty($url))
		{
		   $flg= (strpos($url, "Home/")!==false && strpos($url, "http://")===false); 
		   $data['url']= ($flg?C('WEBURL').C("WXConfigPathUrl").'/':'').$url;
		}

		$data["operationuser"] =UID;
		$data["createtime"] = date("Y-m-d H:i:s");
		
		if (empty($_POST["replyid"])||$_POST["replyid"] == 0) {
			unset ( $data ["replyid"] );
			$result = M ( "wx_reply" )->add ( $data );
		} else {
			$data ["replyid"] = $data ["replyid"]; 
			$result = M ( "wx_reply" )->save ( $data );
		}
		
		$exe_scriptstr = "<script type='text/javascript'>
                setTimeout(function(){
					 var win=   art.dialog.opener;
                     win.location.reload();
                     art.dialog.close();
                },300); 
           </script>";

		if ($result) {
			$this->success ( "操作成功!".$exe_scriptstr);
		}
		else{
			$this->error ( "删除失败！".$exe_scriptstr);
		}
	}

	public function delmessage(){
		$replyid=I("id");
		if(empty($replyid)){
			$this->error("传递参数错误!");
		} 
		$where="FIND_IN_SET(replyid,'$replyid')";
		$result = M("wx_reply")->where($where)->delete();
		if ($result) {
			$this->success("删除成功!");
		}
		else{
			$this->error ( "删除失败！" );
		}
	}

	/*
	public function getbyid(){
		$result = M("wx_reply")->where(array("id"=>$_POST["id"]))->find();
		if ($result) {
			$this->ajaxReturn($result);
		}
	} 
	*/

}
?>
